home *** CD-ROM | disk | FTP | other *** search
- Path: crl.crl.com!not-for-mail
- From: bobfry@crl.com (Robert Fry)
- Newsgroups: comp.lang.c
- Subject: Re: To malloc (new) or not to malloc? When is the question.
- Date: 9 Jan 1996 08:59:51 -0800
- Organization: CRL Dialup Internet Access
- Message-ID: <4cu6u7$btf@crl.crl.com>
- References: <4ctvk3$ort@maverick.tad.eds.com>
- NNTP-Posting-Host: crl.com
-
- fignet05.darrins@eds.com (Darrin Smith) writes:
-
- >Why is it that you can do something like the following:
-
- > char *x;
-
- > x="Some really long string with no particular meaning";
-
- >and have no problems, but can't (safely) do something like this:
-
- > struct st1{char one[10];
- > char two[20];
- > char three[10];
- > };
-
- > st1 *sptr; //or struct st1 *sptr in C instead of C++
-
- > sptr=fread(....);
-
- >What is going on here? Why isn't memory set aside for st1 *sptr just as
- >it is for char *x? Before I did the new (or malloc) it seemed as though
- >my program was getting written over!
-
- Actually, memory is not being set aside for x. What the assignment does
- is set x to point to that string of characters, which are explicitly
- included in your source file. And, in fact, I don't believe you are
- guaranteed to be able to write into that space by the ANSI standard.
- (However, I also believe many compilers will allow you to do so).
-
- In your second example, you never defined a place for sptr to point to,
- so it has some uninitialized value. This is usually something that does
- VERY BAD THINGS. In your first example, you have initialized x to point
- to a well-defined place.
-
- Does this help? Feel free to send email for further clarification.
-
- Bob
-